package vistas2;

import Dominio.Controladores.CtrlConsultaNoticias;
import Dominio.Controladores.CtrlConsultas;
import Dominio.CtrlDominio;
import Dominio.NoEstaNoticiaException;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.io.StreamCorruptedException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author  sandra
 */
public class ConsNoticias extends javax.swing.JFrame {

    private menu menu; 
    private ConsultarTema consTema;
    private boolean bMenu;
    private CtrlConsultaNoticias CD = new CtrlConsultaNoticias();
    private Vector noticias = new Vector<String>(0, 1);
    private Vector temas = new Vector<String>();
    private Vector palabras = new Vector<String>(0, 1);
    private static String[] cabecerasTablaTema = new String[]{"Lista de Temas de la noticia"};
    private static String[] cabecerasTablaPalabra = new String[]{"Lista de Palabras de la noticia"};

    /** Creates new form ConsNoticias */
    public ConsNoticias(menu m) throws StreamCorruptedException, ClassNotFoundException, IOException {
//        String pt = "/home/sandra/Escritorio/diccTema.dat";
        //      String pn = "/home/sandra/Escritorio/diccNot.dat";
        //    String pp = "/home/sandra/Escritorio/diccPal.dat";
        //  CD.cargarDiccionarios(pn, pp, pt, pp);
        menu=m;
        bMenu=true;
        noticias = CD.listarNoticias();
        if (noticias.size() == 0) {
            JOptionPane.showMessageDialog(this, "No es posble consultar las noticias, porque no hay ninguna noticia en el diccionario", "No hay noticias en el diccionario", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
            menu.setVisible(true);
        } else {
            this.setTitle("Consultar Noticia");
            this.setLocationRelativeTo(null);
            initComponents();
            this.setLocationRelativeTo(null);
            for (int tam = 0; tam < noticias.size(); tam++) {
                jComboNoticias.addItem(noticias.get(tam));
            }
        }
    }

    public ConsNoticias(String noticia, ConsultarTema consultarTema) {
        try {
            this.setTitle("Consultar Noticia");
            this.setLocationRelativeTo(null);
            initComponents();
            consTema=consultarTema;
            bMenu=false;
            jComboNoticias.addItem(noticia);

            palabras = CD.listarPalabrasNoticia(noticia);
            Object[][] aux2 = new Object[palabras.size()][1];
            for (int i = 0; i < palabras.size(); i++) {
                aux2[i][0] = palabras.get(i);
            }
            javax.swing.table.DefaultTableModel pp = new javax.swing.table.DefaultTableModel(aux2, cabecerasTablaPalabra);
            jPalabras.setModel(pp);
            
            temas = CD.listarTemasNoticia(noticia);
            Object[][] aux = new Object[temas.size()][1];
            for (int i = 0; i < temas.size(); i++) {
                aux[i][0] = temas.get(i);
            }
            jTemas.setModel(new javax.swing.table.DefaultTableModel(aux, cabecerasTablaTema));
        } catch (NoEstaNoticiaException ex) {
            Logger.getLogger(ConsNoticias.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jComboNoticias = new javax.swing.JComboBox();
        jBtConsNoticia = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTemas = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPalabras = new javax.swing.JTable();
        jBtCerrar = new javax.swing.JButton();
        jBtVisualizar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Noticia: ");

        jComboNoticias.setEditable(true);

        jBtConsNoticia.setText("Consultar noticia");
        jBtConsNoticia.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jBtConsNoticiaMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(63, 63, 63)
                .addComponent(jComboNoticias, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jBtConsNoticia)
                .addContainerGap(62, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addContainerGap(494, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboNoticias, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBtConsNoticia))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(15, 15, 15)
                    .addComponent(jLabel1)
                    .addContainerGap(15, Short.MAX_VALUE)))
        );

        jTemas.setAutoCreateRowSorter(true);
        jTemas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Lista de Temas de la noticia"
            }
        ));
        jTemas.setCellSelectionEnabled(true);
        jTemas.setEditingRow(0);
        jTemas.setName("ejemplo\n"); // NOI18N
        jScrollPane1.setViewportView(jTemas);

        jPalabras.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Lista de Palabras de la noticia"
            }
        ));
        jScrollPane2.setViewportView(jPalabras);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)
        );

        jBtCerrar.setText("Cerrar");
        jBtCerrar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jBtCerrarMouseClicked(evt);
            }
        });

        jBtVisualizar.setText("Visualizar Noticia");
        jBtVisualizar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jBtVisualizarMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(113, 113, 113)
                        .addComponent(jBtCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(62, 62, 62)
                        .addComponent(jBtVisualizar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtCerrar)
                    .addComponent(jBtVisualizar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jBtConsNoticiaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jBtConsNoticiaMouseClicked
        try {
            String noticia = (String) jComboNoticias.getSelectedItem();
            temas = CD.listarTemasNoticia(noticia);
            Object[][] aux = new Object[temas.size()][1];
            for (int i = 0; i < temas.size(); i++) {
                aux[i][0] = temas.get(i);
            }
            jTemas.setModel(new javax.swing.table.DefaultTableModel(aux, cabecerasTablaTema));

            palabras = CD.listarPalabrasNoticia(noticia);
            Object[][] aux2 = new Object[palabras.size()][1];
            for (int i = 0; i < palabras.size(); i++) {
                aux2[i][0] = palabras.get(i);
            }
            jPalabras.setModel(new javax.swing.table.DefaultTableModel(aux2, cabecerasTablaPalabra));
        } catch (NoEstaNoticiaException ex) {
            Logger.getLogger(ConsNoticias.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jBtConsNoticiaMouseClicked

    private void jBtCerrarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jBtCerrarMouseClicked
        this.dispose();
        if(bMenu) menu.setVisible(true);
        else consTema.setVisible(true);
    }//GEN-LAST:event_jBtCerrarMouseClicked

    private void jBtVisualizarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jBtVisualizarMouseClicked
        try {
            String noticia = (String) jComboNoticias.getSelectedItem();
            File path = new File(CD.getPath(noticia));
            Desktop.getDesktop().open(path);
            //new VerNoticia(noticia).setVisible(true);
        } catch (NoEstaNoticiaException ex) {//GEN-LAST:event_jBtVisualizarMouseClicked
            JOptionPane.showMessageDialog(this, "La noticia que desea visualizar no se encuentra en el diccionario", "Error: no se encuentra la noticia", JOptionPane.ERROR_MESSAGE);
        } catch (IOException ex) {
            System.out.println("ha entrat");
        }catch(IllegalArgumentException ex){
            JOptionPane.showMessageDialog(this, "No se puede visualizar la noticia porque se ha movido o borrado", "Error: no se puede visualizar la noticia", JOptionPane.ERROR_MESSAGE);
        }
    }                                          
    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    /*  public void run() {
    try {
    new ConsNoticias(idioma).setVisible(true);
    } catch (StreamCorruptedException ex) {
    Logger.getLogger(ConsNoticias.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
    Logger.getLogger(ConsNoticias.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
    Logger.getLogger(ConsNoticias.class.getName()).log(Level.SEVERE, null, ex);
    }
    }
    });*
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtCerrar;
    private javax.swing.JButton jBtConsNoticia;
    private javax.swing.JButton jBtVisualizar;
    private javax.swing.JComboBox jComboNoticias;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTable jPalabras;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTemas;
    // End of variables declaration//GEN-END:variables
}
